package com.michaelbulava.TFS.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcsHelper;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import com.michaelbulava.TFS.tasks.BackgroundableTask;
import com.michaelbulava.TFS.vcs.TFSVcs;
import com.michaelbulava.TFS.vcs.exceptions.TFSException;
import com.michaelbulava.TFS.vcs.internal.SharedOperations;
import com.michaelbulava.TFS.vcs.internal.WorkstationHelper;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;
import com.michaelbulava.TFS.vcs.utils.TFSUtil;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.core.clients.versioncontrol.specs.version.LatestVersionSpec;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by MichaelA on 10/14/2015.
 */
public class GetLatestVersionAction extends OnlineRequiredAction {
    /**
     * Implement this method to provide your action handler.
     *
     * @param e Carries information on the invocation place
     */
    @Override
    public void actionPerformed(AnActionEvent e) {
        final Project project = (Project)e.getData(CommonDataKeys.PROJECT);
        //final VirtualFile[] files = VcsUtil.getVirtualFiles(e);
        List<FilePath> files = TFSFileUtil.getFilePaths(VcsUtil.getVirtualFiles(e));

        getLatestTask task = new getLatestTask(project, files);
        ProgressManager.getInstance().run(task);
    }

    private static class getLatestTask extends BackgroundableTask{
        private final List<FilePath> myFiles;

        protected getLatestTask(Project project, List<FilePath> files) {
            super(project, "Get Latest Version", true);
            this.myFiles = files;
        }

        @Override
        protected boolean runInBackground() {
            return false;
        }

        @Override
        public void run(final ProgressIndicator indicator) {
            final List<VcsException> errors = new ArrayList<>();
            try {
                WorkstationHelper.processByWorkspaces(myFiles, true, myProject, indicator,
                        new WorkstationHelper.VoidDelegateProcess() {
                            @Override
                            public void executeRequest(Workspace workspace, List<FilePath> localPaths) throws TFSException {
                                errors.addAll(SharedOperations.performGet(myProject, workspace, indicator, localPaths,
                                        LatestVersionSpec.INSTANCE, true));
                                //AbstractVcsHelper.getInstance(myProject).

                            }
                        });
            } catch (TFSException e) {
                errors.add(new VcsException(e));
            }
            if (!errors.isEmpty())
                AbstractVcsHelper.getInstance(getProject()).showErrors(errors, TFSVcs.VCS_NAME);//TFSUtil.collectionException(errors);
        }
    }
}

